package com.atguigu.cloud.controller;

import com.atguigu.cloud.entities.PayDTO;
import com.atguigu.cloud.utils.ResultData;
import jakarta.annotation.Resource;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.net.URI;

/**
 * @Author: huahua
 * @Date: 2024/5/30 0030
 * @Time: 22:56
 * @Description:
 */
@RestController
@RequestMapping("consumer/pay/")
public class OrderController {
    //todo  硬编码写死
    //public static final String PaymentSrv_URL = "http://localhost:8001";

    //Consul服务注册中心上的名称
    public static final String PaymentSrv_URL = "http://cloud-payment-service";

    @Resource
    private RestTemplate restTemplate;

    @GetMapping("add")
    public ResultData addOrder(PayDTO payDTO){
        return restTemplate.postForObject(
                PaymentSrv_URL + "/pay/add",
                payDTO,
                ResultData.class
        );
    }
    @DeleteMapping("del/{id}")
    public ResultData deletePay(@PathVariable("id") Integer id){
//        RequestEntity<?> requestEntity = RequestEntity
//                .delete(PaymentSrv_URL + "/pay/del/" + id)
//                .accept(MediaType.APPLICATION_JSON)
//                .build();
//
//        ResponseEntity<ResultData> response = restTemplate.exchange(
//                requestEntity,
//                ResultData.class
//        );
//        return response.getBody();

        restTemplate.delete(
                PaymentSrv_URL + "/pay/del/" + id,
                ResultData.class
        );



        return ResultData.success(null);
    }

    @PutMapping("update")
    public ResultData updatePay(@RequestBody PayDTO payDTO){

        restTemplate.put(
                PaymentSrv_URL + "/pay/update",
                payDTO,
                ResultData.class
        );

        return ResultData.success(null);
    }

    @GetMapping("get/{id}")
    public ResultData getById(@PathVariable("id") Integer id){
        return restTemplate.getForObject(
                PaymentSrv_URL + "/pay/get/" + id,
                ResultData.class
        );
    }

    @GetMapping("all")
    public ResultData getAll(){
        return restTemplate.getForObject(
                PaymentSrv_URL + "/pay/all",
                ResultData.class
        );
    }

    @GetMapping("/get/info")
    private String getInfoByConsul(){
        return restTemplate.getForObject(
                PaymentSrv_URL + "/pay/getInfo",
                String.class
        );
    }



}
